package com.bjsxt.ego.users.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.ego.common.util.ConstatFinalUtil;
import com.bjsxt.ego.common.util.PageInfoUtil;
import com.bjsxt.ego.users.dao.IAAdminsDao;
import com.bjsxt.ego.users.dao.IARoleDao;
import com.bjsxt.ego.users.pojo.AAdmins;
import com.bjsxt.ego.users.service.IUsersService;

@Service("usersService")
public class UsersServiceImpl implements IUsersService{
	@Resource
	private IAAdminsDao adminsDao;

	@Resource
	private IARoleDao roleDao;
	@Override
	public JSONObject saveOneAdminsService(AAdmins admins) {

		JSONObject resultJSON=new JSONObject();
		/**
		 * 影响条数
		 */
		int res=this.adminsDao.save(admins);
		if(res>0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info",ConstatFinalUtil.INFO_JSON.get("0"));
			
			/*存储data*/
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("id", admins.getId());
			dataJSON.put("effect", res+"");
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code","1");
			resultJSON.put("info",ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}

	@Override
	public AAdmins findOneAdminsService(Map<String,Object> condMap) {
		return this.adminsDao.findOne(condMap);
	}

	@Override
	public List<AAdmins> findAdminsCondListService(PageInfoUtil pageInfoUtil,Map<String,Object>condMap)
	{
		/*模糊查询*/
		if("".equalsIgnoreCase(condMap.get("keyword")+""))
		{
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
		}
		if(pageInfoUtil!=null)
		{
			/*分页*/
			/*查询总记录数*/
			condMap.put("pageCount","true");
			List<AAdmins> adminsList=this.adminsDao.findList(condMap);
			
			/*取总记录数*/
			if(adminsList.size()==1)
			{
				pageInfoUtil.setTotalRecord(adminsList.get(0).getId());
			}
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			return this.adminsDao.findList(condMap);
			
		}
		return this.adminsDao.findList(condMap);
	}

	@Override
	public JSONObject updateOneAdminsService(AAdmins admins) {
		JSONObject resultJSON=new JSONObject();
		
		/*影响条数*/
		int res=this.adminsDao.update(admins);
		if(res>0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("code", ConstatFinalUtil.INFO_JSON.get("0"));
			
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("id", admins.getId());
			dataJSON.put("effect", res+"");
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneAdminsService(AAdmins admins) {
		JSONObject resultJSON=new JSONObject();
		
		int res=this.adminsDao.delete(admins);
		if(res>0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("id", admins.getId());
			dataJSON.put("effect", res+"");
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}

	
	
}
